iT邦幫忙

2024 iThome 鐵人賽

DAY 7
1
IT 管理

30天從版控到code review的實踐指南系列 第 7

Day 7. Git 操作入門:Commit 規則篇。

  • 分享至 

  • xImage
  •  

接下來幾天將介紹關於 Commit 規則、PR template、Conflicts solutions、tag 管理,以及 GitHub Actions 實作自動化版號的完整流程。首先,開發功能時,該如何清楚撰寫 Commits 呢?若能為專案 Commits 訂下規則、保持 Commits 的一致性,可以讓專案成員快速掌握每個開發重點。

從 Conventional Commits 到 Angular 的 Commit Guidelines


Conventional Commits

Conventional Commits 提供了一個撰寫 Commits 的通用原則,在許多開源和私人專案中被廣泛應用。將 Commits 格式結構化的優點:

  • 可結合自動化工具,簡化版本管理流程。
  • 提高 Commits 的易讀性:開發者可快速了解程式碼變更的目的。
    • 使用 標示 breaking change:一眼提醒開發人員,此為重要變更。

      feat!(api): remove deprecated endpoints
      
      1. feat:表示這是一個新功能的 commit。
      2. !(api):驚嘆號表示這是一個 breaking change,影響的範疇是 api。
      3. 原因:deprecated endpoints 已不再適用於專案,請開發人員注意應開發新的 api,若 endpoints 有用到此支已被刪除的 api,需留意及更新程式碼。
      fix!(button): change default style that breaks layout
      
      1. fix:表示這是一個修復功能的 commit。
      2. !(button):一個 breaking change,影響了 button 的功能。
      3. 原因:需修復按鈕的預設樣式,已解決相關 bug。由於系統許多地方都可能用到預設的按鈕樣式,所以此為重大的 commit,需特別注意。
    • 加上主題提供明確提示。

      feat(auth): implement two-factor authentication
      
      1. (auth):與身份驗證(authentication)相關。
      2. 加上 () 原因:特定主題,與權限驗證機制相關。
      3. fix(ui): correct color issue in header
        1. (ui):與使用者介面設計(user interface)相關。
        2. 加上 () 原因:明確區分為 UI 問題,僅修復 UI,與實質功能無關。
    • 整齊、詳細說明。

      1. 加入 breaking change 說明:

        feat: allow custom themes
        BREAKING CHANGE: The theme property is now an object instead of a string.
        
      2. 加入完整段落敘述:

        fix: resolve race condition in requests
        This commit introduces a unique request ID to track the latest request.
        It also dismisses any incoming responses that are not from the latest request.
        

Angular 的 Commit Guidelines

依循 Conventional Commits 的規則,專門針對 Angular 框架的 Commit Guidelines。 commit 撰寫架構簡易、有明確定義:

<type>(<scope>): <short summary>

以下針對 type 說明應用情境與提供範例:

  1. build:影響系統建置或使用套件更新

    build(npm): update npm dependencies to latest versions
    
  2. ci:針對 CI 文件更新

    ci(jenkins): update Jenkinsfile to include test and deploy stages
    
  3. docs:僅限文件更新

    docs(readme): add usage examples for the new API methods
    
  4. perf:提升性能

    perf(api): optimize database queries to reduce load time
    
  5. refactor:程式碼重構

    refactor(user-profile): simplify user profile data fetching logic
    

專案 Commit 規則實例:


主要參考了Git Commit Message 這樣寫會更好,替專案引入規範與範例,覺得寫得很清楚!

[<類型>](<特定範圍>): <功能描述>
                       │
                       └─⫸ 描述正在開發 or 修復的功能。

需求/問題:
  │                     
  └─⫸ 說明 commit 的目的:因應何項需求/為了修復哪個問題。
 
調整項目:
  │                     
  └─⫸ 說明 commit 調整項目。

專案 Commit 撰寫範例:


[fix]:xx案件空間選取

問題:
1. 案件空間選取後,定位按鈕功能依舊定到所有xx

調整項目:
1. 結果若為無空間查詢的結果,定位時,使用空間查詢後的查詢結果陣列渲染資料
2. 因1.的關係,當使用一般查詢時,要將空間查詢後的查詢結果陣列清空
[feat]:新增xx案件

需求:
1. 可新增案件基本資訊
2. 可關聯該案件之都計案

調整項目:
1. 已完成html
2. 可將案件傳入後端

Reference



上一篇
Day 6. 導入專案注意事項與 Git Workflow 總結
下一篇
Day 8. Git 操作入門:建立 PR 範本;程式碼合併 conflicts 如何解決?
系列文
30天從版控到code review的實踐指南13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言